package com.jhd.jiangxi;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.IBinder;
import androidx.appcompat.app.AlertDialog;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

public class UtilHelper {
    public static void hideKeyboard(MotionEvent event, View view,
                                    Activity activity) {
        try {
            if (view != null && view instanceof EditText) {
                int[] location = {0, 0};
                view.getLocationInWindow(location);
                int left = location[0], top = location[1], right = left
                        + view.getWidth(), bootom = top + view.getHeight();
                // 判断焦点位置坐标是否在空间内，如果位置在控件外，则隐藏键盘
                if (event.getRawX() < left || event.getRawX() > right
                        || event.getY() < top || event.getRawY() > bootom) {
                    // 隐藏键盘
                    IBinder token = view.getWindowToken();
                    InputMethodManager inputMethodManager = (InputMethodManager) activity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(token,
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void alertMsg(Context context, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setIcon(R.mipmap.dialog_alert);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        Dialog dialog = builder.create();
        if (!((Activity) context).isFinishing()) dialog.show();

        int color = context.getResources().getColor(R.color.colorCyan300);
        Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        if (btn != null) btn.setTextColor(color);
    }

    @FunctionalInterface
    public interface SelectOK {
        void execute();
    }

    private static void handleOK(SelectOK selectOK) {
        selectOK.execute();
    }

    public static void dialogSelector(Context context, String title, String msg, final SelectOK toDo) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setIcon(R.mipmap.dialog_alert);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                handleOK(toDo);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        if (!((Activity) context).isFinishing()) builder.show();
    }

    public static class Cache {
        public static String HISTORY = "history";
        public static String USER = "user";
        public static String BILL = "bill";
        public static String LOGIN = "login";

        //专门提供给疫苗属性弹窗的传递参数用，因为此对象量比较大
        public static String VACCPROP = "vaccprop";
    }

    public static String DOWNLOAD_RECEIVER = "com.zigin.scan.download.RECEIVER";
    public static String EMIAO_KEY = "2c9KfplCB6sVfAJnEUjZphkM";

    public static class baseUrls {
        private static String url="https://jiangxi.zigin.net/api/";
//        private static String url="http://10.16.130.27:8788/api/";

        public static String EMIAO(String backSystem) {
            if ("0".equals(backSystem)) {//测试
                return "https://jiangxi.zigin.net/api/emiao/";
            } else {//正式
                return url+"emiao/";
            }
        }

        public static String API(String backSystem) {
            if ("0".equals(backSystem)) {//测试
                return "https://jiangxi.zigin.net/api/";
            } else {//正式
                return url;
            }

        }
        public static String UC(String backSystem) {
            if ("0".equals(backSystem)) {//测试
                return "https://jiangxi.zigin.net/api/uc/";
            } else {//正式
                return url+"uc/";
            }
        }
        //版本更新
        public static String VERSION = "http://121.42.143.30:9527/";

        //闪退等异常追踪信息上传地址
        public static String ERR_TRACE = "http://121.42.143.30:9530/";
    }

    public static Retrofit retrofit(String baseUrl) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        return new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(FastJsonConverterFactory.create()).client(builder.build()).build();
    }

    public static Retrofit retrofit(ScanApplication application, String baseUrl) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.addInterceptor(new NetInterceptor(application));
        return new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(FastJsonConverterFactory.create()).client(builder.build()).build();
    }

    public static String getCategoryName(String category) {
        if (category == null) category = "";
        switch (category) {
            case "1":
                return "免规";
            case "2":
                return "非免规";
            case "3":
                return "配套品";
            default:
                return category;
        }
    }

}
